package cn.zhjshn.startproject.model;


import cn.zhjshn.startproject.bean.WeatherResult;
import cn.zhjshn.startproject.iface.WeatherIface;
import cn.zhjshn.startproject.listener.WeatherListener;
import cn.zhjshn.startproject.service.WeatherService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Junsheng on 2016/12/14.
 */

public class WeatherModel extends BaseModel implements WeatherIface {
    private WeatherService service;
    private static WeatherModel model;
    private static final String BASE_URL = "http://www.weather.com.cn/";
    public WeatherModel(String url) {
        super(url);
    }
    public WeatherModel(){
        super(BASE_URL);
    }
    public static WeatherModel getInstance(){
        if(model==null){
            model = new WeatherModel();

        }
        return model;
    }

    @Override
    public void getWeather(String cityNumber,
                           final WeatherListener listener) {
        service = createService(WeatherService.class);
        Call<WeatherResult> call = service.getResult(cityNumber);
        call.enqueue(new Callback<WeatherResult>() {
            @Override
            public void onResponse(Call<WeatherResult> call, Response<WeatherResult> response) {
                if(response.isSuccessful()&&response.body()!=null){
                    listener.onResponse(
                            response.body().getWeatherinfo());
                }
                else{
                    listener.onFail("解析错误！");
                }
            }

            @Override
            public void onFailure(Call<WeatherResult> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }
}
